#include <gtk/gtk.h>
#include <pthread.h>
#include <unistd.h>
#include "net.h"
#include <string.h>
#include <errno.h>


// ----------------------------------------------------------------------------
void
tray_icon_on_click(GtkStatusIcon *status_icon, 
                   gpointer user_data)
{
//    GtkStatusIcon *tray_icon;
  
    printf("Clicked on tray icon\n");
//    printf("user data is %d", user_data);

  
    gtk_status_icon_set_from_file(status_icon,
                                  "/home/overstre/projects/fastweather/firefox/weather/skin/-6.ico");

        
}


// ----------------------------------------------------------------------------
void
tray_icon_on_menu(GtkStatusIcon *status_icon,
                  guint button, 
                  guint activate_time,
                  gpointer user_data)
{
    printf("Popup menu\n");
    gtk_status_icon_set_from_file(status_icon,
                                  "/home/overstre/projects/fastweather/firefox/weather/skin/weather.ico");
}


void
updateIcon(GtkStatusIcon *tray_icon)
{
    char file[256];
    char tooltip[128];
    float temp = getTemp();

    
    sprintf(file, "../res/%d.ico", (int) temp);
    gtk_status_icon_set_from_file(tray_icon, file);
/*     gtk_status_icon_set_visible(tray_icon, FALSE); */
/*     gtk_status_icon_set_visible(tray_icon, TRUE); */

    printf("updating icon to %g\n", temp);

    sprintf(tooltip, "Boulder Temperature is %2.1f", temp);
    


    //    gtk_status_icon_set_visible(tray_icon, FALSE);    
    //    gtk_status_icon_set_visible(tray_icon, TRUE);

    //    gtk_main_iteration();

/*     gtk_signal_emit_by_name((GtkObject *)tray_icon, */
/*                             "activate"); */

    gtk_status_icon_set_tooltip(tray_icon, tooltip);

}


// ----------------------------------------------------------------------------
static GtkStatusIcon *
create_tray_icon()
{
    GtkStatusIcon *tray_icon;

    tray_icon = gtk_status_icon_new();

    g_signal_connect(G_OBJECT(tray_icon), "activate", 
                     G_CALLBACK(tray_icon_on_click), NULL);
    g_signal_connect(G_OBJECT(tray_icon), 
                     "popup-menu",
                     G_CALLBACK(tray_icon_on_menu), NULL);
    gtk_status_icon_set_from_icon_name(tray_icon,
                                       GTK_STOCK_MEDIA_STOP);
/*     gtk_status_icon_set_from_file(tray_icon, */
/*                                   "../res/weather.ico");         */
    updateIcon((GtkStatusIcon *) tray_icon);    
    gtk_status_icon_set_tooltip(tray_icon, 
                                "Current Temperature is unknown.");
    gtk_status_icon_set_visible(tray_icon, TRUE);


// update to current temp
//    update

    
    return tray_icon;
}


void *
updateTask(void *tray_icon)
{
    while(1)
    {
      printf("updating temp");
      updateIcon((GtkStatusIcon *) tray_icon);
//      gtk_status_icon_set_blinking((GtkStatusIcon *)tray_icon, true);
      sleep(300);
    }
}

// ----------------------------------------------------------------------------
int
main(int argc, char **argv)
{
    GtkStatusIcon *tray_icon;
    pthread_t task;

    gtk_init(&argc, &argv);
    tray_icon = create_tray_icon();

    if(pthread_create(&task, NULL, updateTask, (void *) tray_icon) != 0)
    {
      printf("Error creating thread %s\n", strerror(errno));
    }
    
    
//    gtk_main();
    while(1)
    {
    gtk_main_iteration_do(1);
    }
    //  while(gtk_events_pending()) gtk_main_iteration();

    return 0;
}
